package appapi.controller.oider;

import appapi.Request.Basic.DelEquipTypeReq;
import appapi.Vo.AddEquipTypeVo;
import appapi.Vo.EditEquipTypeVo;
import appapi.Vo.bigScreen.EquipTypeTreeVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.MyLog;
import appapi.dto.basic.EquipTypeVo;
import appapi.dto.basic.GenerateImportDto;
import appapi.entity.basic.EquipmenttypeEntity;
import appapi.service.EquipmenttypeService;
import appapi.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/27 16:48
 */
@Api(tags = "基础设置-设备类型")
@RestController
@RequestMapping("/api/equipType")
public class EquipTypeController {
    private static final Logger logger = LoggerFactory.getLogger(EquipTypeController.class);
    @Autowired
    private EquipmenttypeService equipmenttypeService;
    /**
     * 列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<EquipmenttypeEntity> list(@Valid String equipTypeName) {
        try {
            List<EquipmenttypeEntity> dataList = equipmenttypeService.queryList(equipTypeName);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取列表异常", e);
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 列表
     */
    @GetMapping("/getTree")
    @ApiOperation(value = "获取树")
    public ResponseBean<EquipTypeTreeVo> getTree(@Valid Long equipTypeId) {
        try {
            List<EquipTypeTreeVo> dataList = equipmenttypeService.getTreeLazy(equipTypeId);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取树异常", e);
            return ResponseBean.fail("获取树异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equipTypeId", dataType = "long", value = "设备类别Id",required = true)
    })
    public ResponseBean<EquipmenttypeEntity> detail(@Valid Long equipTypeId) {
        try {
            if (equipTypeId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            EquipmenttypeEntity entity = equipmenttypeService.selectByPrimaryKey(equipTypeId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("未查询到记录");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增信息")
    @PostMapping(value = "/addInfo")
    @MyLog(title = "系统设置新增信息",businessType = BusinessTypeEnum.insert)
    public ResponseBean addInfo(@Validated @RequestBody AddEquipTypeVo vo) {
        try {
            EquipmenttypeEntity entity = vo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = equipmenttypeService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复添加");
            }
            int iRet = equipmenttypeService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            logger.error("新增异常", e);
            return ResponseBean.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改信息")
    @PostMapping(value = "/editInfo")
    @MyLog(title = "系统设置修改信息",businessType = BusinessTypeEnum.update)
    public ResponseBean update(@Validated @RequestBody EditEquipTypeVo vo) {
        try {
            EquipmenttypeEntity entity = vo.vo2Entity();
            entity.setUpdateTime(LocalDateTime.now());
            int count = equipmenttypeService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复修改");
            }
            int iRet = equipmenttypeService.updateByPrimaryKey(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @PostMapping(value = "/deleteInfo")
    @MyLog(title = "系统设置删除信息",businessType = BusinessTypeEnum.delete)
    public ResponseBean delete(@Validated @RequestBody DelEquipTypeReq req) {
        try {
            if (req.getEquipTypeId() == null) {
                return ResponseBean.fail("ID不能为空");
            }
            int iRet = equipmenttypeService.deleteByPrimaryKey(req.getEquipTypeId());
            if (iRet == -1) {
                return ResponseBean.fail("存在引用不能删除");
            }
            else if (iRet == -2) {
                return ResponseBean.fail("对象不存在");
            }
            else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除异常", e);
            return ResponseBean.fail("删除异常");
        }
    }

    @ApiOperation(value = "导入设备类型")
    @PostMapping("/importEquipType")
    @MyLog(title = "导入设备类型",businessType = BusinessTypeEnum.eximport)
    public ResponseBean importEquipType(@RequestParam("file") MultipartFile file) {
        try {
            List<EquipTypeVo> list = ExcelUtils.importExcel(file, 1, 1, true, EquipTypeVo.class);
            if (list == null) {
                return ResponseBean.fail("数据为空");
            }
            if (list.size() == 0) {
                return ResponseBean.fail("数据为空");
            }
            GenerateImportDto model = equipmenttypeService.importTypeList(list);
            if (model != null) {
                return ResponseBean.success(model);
            } else {
                return ResponseBean.fail();
            }
        } catch (Exception e) {
            //logger.error("导入设备类型出错", e);
            return ResponseBean.fail("导入设备类型出错");
        }
    }
}
